<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="emitBirthday"
  />
</template>

<script>
import * as dayjs from "dayjs";
import { emitUserFile } from "@/api/user.js";

export default {
  data() {
    return {
      minDate: new Date(1949, 10, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value),
    };
  },
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  methods: {
    async emitBirthday() {
      let day = dayjs(this.currentDate).format("YYYY-MM-DD");
      if (day === this.value) return;
      try {
        await emitUserFile({
          birthday: day,
        });
        this.$toast.success("更新成功");
        this.$bus2.$emit("delBirthday", true);
      } catch (error) {
        this.$toast("更新失败");
      }
    },
  },
};
</script>

<style>
</style>